package com.xxxx.manager.service.impl;

import com.xxxx.api.vo.GoodsCategoryTree;
import com.xxxx.manager.mapper.GoodsCategoryMapper;
import com.xxxx.manager.pojo.GoodsCategory;
import com.xxxx.manager.service.IGoodsCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ppl
 * @since 2021-01-01
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements IGoodsCategoryService {

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    @Cacheable(value = "goodsCategory", key="#pId")
    public List<GoodsCategory> selectCategoryParentId(Integer pId) {
        Map<String, Object> params = new HashMap<>();
        params.put("parent_id", pId);
        List<GoodsCategory> goodsCategories =  goodsCategoryMapper.selectByMap(params);
        return goodsCategories;
    }

    @Override
    @Transactional
    public int saveCategory(GoodsCategory goodsCategory) {
        //GoodsCategory(id=null, name=手机 、 数码 、 通信123, mobileName=数码产品321, parentId=35, parentIdPath=null, level=3, sortOrder=12, isShow=1, image=, isHot=null, catGroup=1, commissionRate=12)
        //parentIdPath  赋值
        String parentPath=goodsCategoryMapper.getParentIdPath(goodsCategory.getParentId());
        Integer maxID = goodsCategoryMapper.getMaxID()+1;
        String parentIdPath=parentPath+"_"+maxID;
        goodsCategory.setParentIdPath(parentIdPath);
        return goodsCategoryMapper.insert(goodsCategory);
    }

    @Override
    public List<GoodsCategoryTree> showTreeGoodsCategory() {
        return goodsCategoryMapper.selectTreeGoodsCategory(0);
    }


    public List<Integer> getAllGroup(){
        return goodsCategoryMapper.selectCatGroupList();
    }
}
